package com.itheima.mall.web.pms;

import com.itheima.mall.dto.ProductAttributeDto;
import com.itheima.mall.pojo.pms.ProductAttribute;
import com.itheima.mall.pojo.pms.ProductAttributeCategory;
import com.itheima.mall.service.pms.ProductAttributeService;
import com.itheima.mall.vo.AttrInfoVo;
import com.itheima.mall.vo.PageResult;
import com.itheima.mall.vo.ProductAttributeCategoryVo;
import com.itheima.mall.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productAttribute")
public class ProductAttributeController {
    @Autowired
    private ProductAttributeService productAttributeService;

    /**
     * 添加商品属性分类
     * @param name
     * @return
     */
    @PostMapping("/category/create")
    public R categoryCreate(@RequestParam String name) {
        Boolean aBoolean = productAttributeService.categoryCreate(name);
        return aBoolean?R.succeed(null, "操作成功"):R.error("商品类型已存在");
    }

    /**
     * 删除单个商品属性分类
     * @param id
     * @return
     */
    @GetMapping("category/delete/{id}")
    public R categoryDelete(@PathVariable("id") Integer id) {
        Boolean result = productAttributeService.categoryDelete(id);
        return result?R.succeed(null, "操作成功"):R.error("商品属性正在被使用,不能删除");
    }

    /**
     * 分页获取所有商品属性分类
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/category/list")
    public R categoryList(@RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "5") Integer pageSize) {
        PageResult result = productAttributeService.categoryList(pageNum, pageSize);
        return R.succeed(result, "操作成功");
    }

    /**
     * 获取所有商品属性分类及其下属性
     * @return
     */
    @GetMapping("/category/list/withAttr")
    public R categoryListWithAttr() {
        List<ProductAttributeCategoryVo> list = productAttributeService.categoryListWithAttr();
        return R.succeed(list, "操作成功");
    }

    /**
     * 修改商品属性分类
     * @param id
     * @param name
     * @return
     */
    @PostMapping("/category/update/{id}")
    public R categoryUpdate(@PathVariable("id") Integer id,
                            @RequestParam String name) {
        productAttributeService.categoryUpdate(id, name);
        return R.succeed(null, "操作成功");
    }

    /**
     * 获取单个商品属性分类信息
     * @param id
     * @return
     */
    @GetMapping("/category/{id}")
    public R selectCategory(@PathVariable("id") Integer id) {
        ProductAttributeCategory productAttributeCategory = productAttributeService.selectCategory(id);
        return R.succeed(productAttributeCategory, "操作成功");
    }

    /**
     * 添加商品属性信息
     *
     * @param
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody ProductAttributeDto productAttributeDto) {
        productAttributeService.creat(productAttributeDto);
        return R.succeed(null, "操作成功");
    }

    /**
     * 批量删除商品属性
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestParam Integer[] ids) {
        productAttributeService.delete(ids);
        return R.succeed(null, "操作成功");
    }

    /**
     * 修改商品属性信息
     * @param id
     * @param productAttribute
     * @return
     */
    @PostMapping("update/{id}")
    public R update(@PathVariable("id") Integer id,
                    @RequestBody ProductAttribute productAttribute){
        productAttributeService.update(id,productAttribute);
        return R.succeed(null,"操作成功");
    }

    /**
     * 查询单个商品属性
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R findById(@PathVariable("id") Integer id) {
        ProductAttribute productAttribute = productAttributeService.findById(id);
        return R.succeed(productAttribute, "查询成功");
    }

    /**
     * 根据分类查询属性列表或参数列表
     * @param cid
     * @param pageNum
     * @param pageSize
     * @param type
     * @return
     */
    @GetMapping("/list/{cid}")
    public R listCid(@PathVariable("cid") Integer cid,
                     @RequestParam(defaultValue = "1") Integer pageNum,
                     @RequestParam(defaultValue = "5") Integer pageSize,
                     @RequestParam Integer type){
        PageResult result = productAttributeService.listCid(cid,pageNum,pageSize,type);
        return R.succeed(result,"操作成功");
    }

    /**
     * 商品分类的id获取商品属性及属性分类
     * @param productCategoryId
     * @return
     */
    @GetMapping("/attrInfo/{productCategoryId}")
    public R attrInfo(@PathVariable("productCategoryId") Integer productCategoryId){
        List<AttrInfoVo> list = productAttributeService.attrInfo(productCategoryId);
        return R.succeed(list,"操作成功");
    }
}